import Vue from 'vue'
import Router from 'vue-router'
import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'


Vue.use(Router)
const routes = [{
        path: '/',
        name: 'Home',
        component: () =>
            import ('@/pages/Home/home')
    },
    {
        path: '/login',
        name: 'Login',
        component: () =>
            import ('@/pages/Login/login')
    },
    {
        path: '/register',
        name: 'Register',
        component: () =>
            import ('@/pages/Login/register')
    },
    {
        path: '/home',
        name: 'Home1',
        component: () =>
            import ('@/pages/Home/home'),

    },
    {
        path: "/category",
        name: "Category",
        component: () =>
            import ("@/pages/Home/Category/category")
    },
    {
        path: '/shop',
        name: 'Shop',
        component: () =>
            import ('@/pages/Shop/shop'),
        meta: {
            title: '',
            requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
        },
    },
    {

        path: "/order",
        name: "Order",
        component: () =>
            import ('@/pages/Shop/order/order'),
    },
    {
        path: "/site",
        name: 'Site',
        component: () =>
            import ('@/pages/Shop/order/siteRun')

    },
    {
        path: '/wallet',
        name: 'Wallet',
        component: () =>
            import ('@/pages/Wallet/wallet')
    },
    {
        path: '/message',
        name: 'Message',
        component: () =>
            import ('@/pages/Message/message')
    },
    {
        path: '/profile',
        name: 'Profile',
        component: () =>
            import ('@/pages/Profile/profile')
    },
    {
        path: '/shezhi',
        name: 'Shezhi',
        component: () =>
            import ('@/pages/Profile/set/set')
    },
    {
        path: '/address',
        name: 'Address',
        component: () =>
            import ('@/pages/Profile/address')
    },
    {
        path: '/account',
        name: 'Account',
        component: () =>
            import ('@/pages/Profile/set/account')
    },
    {
        path: '/phone',
        name: 'Phone',
        component: () =>
            import ('@/pages/Profile/set/phone')
    },
    {
        path: '/pwd',
        name: 'Pwd',
        component: () =>
            import ('@/pages/Profile/set/pwd')
    },
    {
        path: '/editaddress',
        name: 'Aditaddress',
        component: () =>
            import ('@/pages/Profile/editaddress')
    },
    {

        path: "/searchlist",
        name: "SearchList",
        component: () =>
            import ('@/pages/Home/SearchList/searchlist')
    },
    {

        path: "/detail",
        name: "Detail",
        component: () =>
            import ('@/pages/Detail/detail')
    },
    {
        path: "/myorder",
        name: "Myorder",
        component: () =>
            import ('@/pages/Myorder/myorder')
    },
    {
        path: "/gopaid",
        name: "gopaid",
        component: () =>
            import ('@/pages/Myorder/Tobepaid/gopaid')
    },
    {
        path: "/sign",
        name: "Sign",
        component: () =>
            import ('@/pages/Wallet/Sign/sign')
    },
    {
        path: "/tixian",
        name: "walletFoChild",
        component: () =>
            import ('@/pages/Wallet/WalletChild/walletFoChild/walletWu')
    },
    {
        path: '/goodthing',
        name: 'Goodthing',
        component: () =>
            import ('@/pages/Goodthing/goodthing'),

    },
    {
        path: '/mock',
        name: 'finance',
        component: () =>
            import ('@/pages/finance/transcationflow'),
    },
    {
        path: '/bianjiaddress',
        name: 'bianji',
        component: () =>
            import ('@/pages/Profile/bianjiaddress'),
    }
    // {
    //     path: '/goodone/:id',
    //     name: 'Goodchild',
    //     component: () =>
    //         import('@/pages/Goodthing/Goodchild/goodChildOne'),
    //         children:[
    //             {
    //                 path:'tjone',
    //                 component: () =>
    //                 import('@/pages/Goodthing/Goodchild/tjone'),
    //             }
    //         ]

    // },

]

let router = new Router({
    routes,
})
router.beforeEach((to, from, next) => {
    Nprogress.start();
    next()
})
router.afterEach((to, from) => {
    Nprogress.done();
})

// router.beforeEach((to, from, next) => {
//     // ...
//     // NProgress.start();
//     if (from.path == "/shop") {
//         next();
//         console.log(222);
//     }
// })
router.beforeEach((to, from, next) => {
    if (to.matched.some(record => record.meta.requireAuth)) { // 判断该路由是否需要登录权限
        let token = sessionStorage.getItem('uname');
        if (token) { // 判断当前的token是否存在
            next();
        } else {
            next({
                path: '/login',
                // query: { redirect: to.fullPath } // 将跳转的路由path作为参数，登录成功后跳转到该路由
            })
        }
    } else {
        next();
    }
});


export default router